﻿using AutoMapper;
using JinShun.OA.UI.Apis;
using JinShun.OA.UI.Auth;
using JinShun.OA.UI.Dtos.Post;
using JinShun.OA.UI.Dtos.User;
using JinShun.OA.UI.IServer.User;
using JinShun.OA.UI.Models;
using JinShun.OA.UI.Urls;
using Microsoft.Win32;
using Newtonsoft.Json.Linq;

namespace JinShun.OA.UI.Services.User
{
    public class UserService : IUser
    {
        private readonly TokenHelper tokenHelper;
        private readonly HttpWebClient http;
        public UserService(TokenHelper tokenHelper, HttpWebClient http)
        {
            this.tokenHelper = tokenHelper;
            this.http = http;
        }

        public async Task<List<UserModel>> GetUsersAsync()
        {
            List<UserModel> list = new List<UserModel>();
            var token = await tokenHelper.GetTokenAsync();
            UserGetListOutputDto um = await http.ExecuteAsync<UserGetListOutputDto>(UrlBases.User+ "?Id=4522611c-3c59-0c1c-c3b5-3a15059314fb", HttpMethod.Get, null, token);
            // 配置映射
            var config = new MapperConfiguration(cfg => { cfg.CreateMap<UserItems, UserModel>(); });
            var mapper = config.CreateMapper();
            // 执行转换
            list = mapper.Map<List<UserItems>, List<UserModel>>(um.items);
            return list;
        }
        public  List<UserModel> GetUsers()
        {
            List<UserModel> list = new List<UserModel>();
            UserGetListOutputDto um = http.Execute<UserGetListOutputDto>(UrlBases.User + "?UserName=cc", HttpMethod.Get, null, null);
            // 配置映射
            var config = new MapperConfiguration(cfg => { cfg.CreateMap<UserItems, UserModel>(); });
            var mapper = config.CreateMapper();
            // 执行转换
            list = mapper.Map<List<UserItems>, List<UserModel>>(um.items);
            return list;
        }

        public async Task CreateUserAsync(RegisterDto user)
        {
            var token = await tokenHelper.GetTokenAsync();
            await http.ExecuteAsync(UrlBases.LoginInfo_Reques+ "/Register", HttpMethod.Post,Newtonsoft.Json.JsonConvert.SerializeObject(user), token);
        }
    }
}
